import { fetchChannelList, addChannel, updateChannel, deleteChannel } from '../services/contentService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';

export default {
  namespace: 'channel',

  state: {
    channelList: [],
    pagination: { current: 1, pageSize: 10 },
  },

  effects: {
    *fetchChannelList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchChannelList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'channelListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addChannel({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(addChannel, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('添加成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *updateChannel({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(updateChannel, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('修改成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *deleteChannel({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(deleteChannel, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('删除成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },
  },

  reducers: {
    channelListReducer(state, action) {
      return {
        ...state,
        channelList: action.payload.data.dataList,
        pagination: dealPagination(action.payload.data.pageInfo),
      };
    },
  },
};
